home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8573 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  91 lines

  1. Newsgroups: comp.lang.c
  2. Path: uu4news.netcom.com!zodiac!szh
  3. From: szh@zcon.com (Syed Zaeem Hosain)
  4. Subject: Re: Calling a Function Twice from a printf statement.
  5. Message-ID: <1996Mar5.022348.2491@zcon.com>
  6. Sender: szh@zcon.com (Syed Zaeem Hosain)
  7. Nntp-Posting-Host: zodiac
  8. Reply-To: szh@zcon.com
  9. Organization: Z Consulting Group
  10. References: <4he6q9$6r6@newsbf02.news.aol.com>
  11. Date: Tue, 5 Mar 1996 02:23:48 GMT
  12.  
  13. In article <4he6q9$6r6@newsbf02.news.aol.com>, razine@aol.com (Razine) writes:
  14. >I am trying to call a function twice from my program on the same printf
  15. >line and have run into a small problem.  i was wondering if someone can
  16. >explain how to fix it.
  17. >
  18. >
  19. >void main(void) {
  20. >
  21. >   printf("First Number is %d , the Second s %d \n",num(0),num(1));
  22. >
  23. >}
  24. >
  25. >int num(int index) {
  26. >   int return_number;
  27. >   
  28. >   switch(index) {
  29. >       case 0 : return_number=0;
  30. >       case 1 : return_number=1;
  31. >                       }
  32. >   return(return_number);
  33. >}
  34. >
  35. >
  36. >Now with this function, the printf line will display 0 for the first one
  37. >and garbage for the second one..  I then thought if I made the variable
  38. >return_number a static variable it would have fixed it but then when i
  39. >made the change the printf line would give me zero for both of them.  Any
  40. >ideas?
  41.  
  42. A few potential problems. First, you do not handle the case of when
  43. 'index' is neither 0 nor 1 - this could result in some uninitialized
  44. value being returned. And, second, in the switch, the case check for 0
  45. drops through to the case check for 1 since there is no break.
  46.  
  47. In this case, the issue of static variables is not important. The
  48. variable is returned by value.
  49.  
  50. Finally, to meet ANSI C standards, do declare 'main' as returning int,
  51. and return a value to the calling OS or program. So, try the following
  52. instead of what you have (pardon the slight change in style):
  53.  
  54. zodiac{108}szh: cat foo.c
  55. #include <stdio.h>
  56.  
  57. int main(void)
  58. {
  59.   printf("First Number is %d , the Second is %d \n",num(0),num(1));
  60.   return(0);
  61. }
  62.  
  63. int num(int index)
  64. {
  65.   int return_number; /* You could also initialize here instead of
  66.                         using the default in the switch */
  67.   switch(index)
  68.     {
  69.     case 0:
  70.       return_number=0;
  71.       break;
  72.     case 1:
  73.       return_number=1;
  74.       break;
  75.     default:
  76.       return_number=-1;
  77.     }
  78.   return(return_number);
  79. }
  80. zodiac{109}szh: gcc -o foo foo.c
  81. zodiac{110}szh: foo
  82. First Number is 0 , the Second is 1 
  83. zodiac{111}szh: 
  84.  
  85.  
  86. -- 
  87. -------------------------------------------------------------------------
  88. | Syed Zaeem Hosain          P. O. Box 610097            (408) 441-7021 |
  89. | Z Consulting Group        San Jose, CA 95161             szh@zcon.com |
  90. -------------------------------------------------------------------------
  91.